package cqut.gbq.qimo.service.impl;

import cqut.gbq.qimo.entity.*;
import cqut.gbq.qimo.mapper.AssignmentMapper;
import cqut.gbq.qimo.mapper.CourseMapper;
import cqut.gbq.qimo.service.AssignmentService;
import cqut.gbq.qimo.service.CourseService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName CourseServiceImpl
 * @Description TODO
 * @Author guobenqi
 * @Date 2021/6/18 10:22
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private AssignmentMapper assignmentMapper;

    @Override
    public List<Course> getClassInfo(Integer sid) {
        return courseMapper.getClassAndAssignment(sid);
    }

    @Override
    public ResBean getCourseInfo(Integer cid) {
        Course course = courseMapper.getCourseById(cid);

        return ResBean.success("获取成功",course);
    }

    @Override
    public ResBean getCourseCount(Integer cid) {

        List<Student> studentList = courseMapper.getCourseCount(cid);
        return ResBean.success("获取成功",studentList);
    }

    @Override
    public ResBean getAssignment(Integer cid,Integer sid) {
        List<Assignment> assignmentList = courseMapper.getAssignment(cid,sid);
        return ResBean.success("获取任务成功",assignmentList);
    }

    @Override
    public ResBean getStuWork(Integer aid, Integer sid) {
        Assignment assignment =  courseMapper.getStuWork(aid,sid);
        return ResBean.success("获取作业成功",assignment);
    }

    @Override
    public ResBean createCourse(Course course) {
        Integer count = courseMapper.createCourse(course.getTeacherId(),course.getCourseName(),course.getCourseClass(),course.getCourseYear(),course.getCourseTerm(),course.getCourseCode());

        if (count > 0 ){
            return ResBean.success("创建课程成功!");
        }else{
            return ResBean.error("创建课程失败!");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Course> getClassInfoByTid(Integer tid) {
        return courseMapper.getClassInfoByTid(tid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCourseByCid(Integer tid,Integer cid) {
        Integer row = courseMapper.deleteCourse(tid,cid);


        List<Integer> aidList = courseMapper.findCourseAid(cid);
        for (Integer aid: aidList){
            assignmentMapper.deleteAssignmentAndStudentByAid(aid);
        }
        assignmentMapper.deleteAssignmentByCid(cid);
        courseMapper.deleteCourseByCid(cid);
        if (row == 1){
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<Assignment> getAssignmentLinkCourse(Integer cid) {
        List<Assignment> assignmentList = courseMapper.getAssignmentLinkCourse(cid);
        if (assignmentList != null){
            return assignmentList;
        }
        return null;
    }

    @Override
    public Integer getPeopleNumber(Integer course_id) {
        Integer number = courseMapper.getPeopleNumber(course_id);
        if (number >= 0){
            return number;
        }
        return null;
    }

    @Override
    public Integer getNotSubmit(Integer aid) {
        return courseMapper.getNotSubmit(aid);
    }

    @Override
    public Integer getNotCheck(Integer aid) {
        return courseMapper.getNotCheck(aid);
    }

    @Override
    public Integer getCheck(Integer aid) {
        return courseMapper.getCheck(aid);
    }

    @Override
    public ResBean editCourseInfo(Course course) {
        Integer count = courseMapper.editCourseInfo(course.getCid(),course.getCourseName(),course.getCourseClass(),course.getCourseYear(),course.getCourseTerm());
        if (count > 0){
            return ResBean.success("修改课程成功!");
        }else{
            return ResBean.error("修改失败");
        }
    }

    @Override
    public Integer getAssIsHandIn(Integer aid, Integer sid) {

        return courseMapper.getAssignmentISHanin(aid,sid);
    }

    @Override
    public Integer getScore(Integer aid, Integer sid) {
        return courseMapper.getAssignmentScore(aid,sid);
    }

    @Override
    public Integer getIscheck(Integer aid, Integer sid) {
        return courseMapper.getIsCheck(aid,sid);
    }

    @Override
    public Integer updateTop(Integer cid) {
        return courseMapper.updateTop(cid);
    }

    @Override
    public Integer updateNoTop(Integer cid) {
        return courseMapper.updateNoTop(cid);
    }

    @Override
    public List<Course> getCourseTop() {
        List<Course> courseTop = courseMapper.getCourseTop();
        if (courseTop!=null){
            return courseTop;
        } else {
            return null;
        }
    }

}
